package com.example.secondhandspringboot.utils;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class MailUtil {

    @Autowired
    private JavaMailSender mailSender;

    //发件人
    @Value("${spring.mail.username}")
    private String sendMailer;

    //发件人昵称
    @Value("${spring.mail.nikeName}")
    private String sendNikeName;

    @Async
    public void sendMailMessage(List<String> to, String code, int methods) {
        try {
            //true代表支持复杂的类型
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true);
            //邮件发信人
            try {
                mimeMessageHelper.setFrom(sendMailer,sendNikeName);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
            //邮件收信人1或多个
            mimeMessageHelper.setTo(to.toArray(new String[0]));
            if (methods==0){
                //注册
                //邮件主题
                mimeMessageHelper.setSubject("注册 校林二手交易圈-平台验证码");
            } else if (methods==1) {
                //登录
                //邮件主题
                mimeMessageHelper.setSubject("登录 校林二手交易圈-平台验证码");
            } else if (methods==3) {
                //重置密码
                mimeMessageHelper.setSubject("重置密码 校林二手交易圈-平台验证码");
            } else if (methods == 4) {
                //修改邮箱
                mimeMessageHelper.setSubject("修改邮箱 校林二手交易圈-平台验证码");
            }
            // 使用HTML模板，并在其中加入样式
            String text = "<html><body display: flex;\n" +
                    "    justify-content: center;\n" +
                    "    align-items: center;\n" +
                    "    height: 100vh;\n" +
                    "    margin: 0;style='font-family: Arial, sans-serif;'>" +
                    "<div text-align=center;><h2 style='color: #008CBA;'>【校林二手交易圈】</h2>" +
                    "<p style='color: #333333; font-size: 16px;'><span style='color: red;'><b>"+code+"</b></span> 为本次验证的验证码，请在5分钟内完成验证。</p>" +
                    "<p style='color: #666666; font-size: 14px;'>为保证账号安全，请勿泄漏此验证码。</p>" +
                    "</div></body></html>";

            //邮件内容
            mimeMessageHelper.setText(text, true);
            //邮件发送时间
            mimeMessageHelper.setSentDate(new Date());

            //发送邮件
            mailSender.send(mimeMessageHelper.getMimeMessage());

            log.info("发送成功==================");
        } catch (MessagingException e) {
            log.info("发送失败==================");
            log.error(e.getMessage());
        }
    }
}
